package com.inei.eea2014.model.dao;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.inei.eea2014.model.connection.UtilDB;
import com.inei.eea2014.model.entity.Caratula;

public class CaratulaDao implements ICRUD<Caratula> {

	// variables
	private UtilDB util;
	private SQLiteDatabase db;
	private Cursor cursor = null;

	// constructor
	public CaratulaDao(Context context) {
		util = new UtilDB(context);
	}

	// metodos del CRUD
	@Override
	public Caratula buscar(Caratula o) {
		Caratula obj = null;
		db = util.getReadableDatabase();

		cursor = db.rawQuery(
				"select * from Caratula where RUC=? and USUARIO=?",
				new String[] { o.getRUC() + "", o.getUSUARIO() + "" });
		if (cursor.moveToFirst()) {
			obj = new Caratula();
			obj.setRUC(cursor.getString(cursor.getColumnIndex("RUC")));
			obj.setUSUARIO(cursor.getString(cursor.getColumnIndex("USUARIO")));
			obj.setC0000_001_01_T(cursor.getString(cursor
					.getColumnIndex("C0000_001_01_T")));
			obj.setC0000_001_02_T(cursor.getString(cursor
					.getColumnIndex("C0000_001_02_T")));
			obj.setC0000_001_03_T(cursor.getString(cursor
					.getColumnIndex("C0000_001_03_T")));
			obj.setC0000_001_04_T(cursor.getString(cursor
					.getColumnIndex("C0000_001_04_T")));
			obj.setC0000_001_05_T(cursor.getString(cursor
					.getColumnIndex("C0000_001_05_T")));
			obj.setC0000_001_06_T(cursor.getString(cursor
					.getColumnIndex("C0000_001_06_T")));
		}

		Cerrar();
		return obj;
	}

	@Override
	public List<Caratula> leer() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int crear(Caratula o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();

		ContentValues valores = new ContentValues();
		valores.put("RUC", o.getRUC());
		valores.put("USUARIO", o.getUSUARIO());
		valores.put("C0000_001_01_T", o.getC0000_001_01_T());
		valores.put("C0000_001_02_T", o.getC0000_001_02_T());
		valores.put("C0000_001_03_T", o.getC0000_001_03_T());
		valores.put("C0000_001_04_T", o.getC0000_001_04_T());
		valores.put("C0000_001_05_T", o.getC0000_001_05_T());
		valores.put("C0000_001_06_T", o.getC0000_001_06_T());

		resultado = (int) db.insert("Caratula", null, valores);
		if (resultado > 0) {
			db.setTransactionSuccessful();
		}

		valores.clear();
		Cerrar();
		return resultado;
	}

	@Override
	public int actualizar(Caratula o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();

		ContentValues valores = new ContentValues();
		valores.put("C0000_001_01_T", o.getC0000_001_01_T());
		valores.put("C0000_001_02_T", o.getC0000_001_02_T());
		valores.put("C0000_001_03_T", o.getC0000_001_03_T());
		valores.put("C0000_001_04_T", o.getC0000_001_04_T());
		valores.put("C0000_001_05_T", o.getC0000_001_05_T());
		valores.put("C0000_001_06_T", o.getC0000_001_06_T());

		resultado = (int) db.update("Caratula", valores, "RUC=? and USUARIO=?",
				new String[] { o.getRUC() + "", o.getUSUARIO() + "" });
		if (resultado > 0) {
			db.setTransactionSuccessful();
		}

		valores.clear();
		Cerrar();
		return resultado;
	}

	@Override
	public int eliminar(Caratula o) {
		// TODO Auto-generated method stub
		return 0;
	}

	// otros metodos
	private void Cerrar() {
		if (cursor != null) {
			cursor.close();
		}
		if (db.inTransaction()) {
			db.endTransaction();
		}
		if (db.isOpen()) {
			db.close();
		}
	}
}
